package com.atguigu.spring.config;

import com.atguigu.spring.interceptors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration // 标记为配置类
public class DemoConfig implements WebMvcConfigurer {

    @Autowired
    private MyInterceptor01 myInterceptor01;

    @Autowired
    private MyInterceptor02 myInterceptor02;

    @Autowired
    private MyInterceptor03 myInterceptor03;

    @Autowired
    private TargetInterceptor01 targetInterceptor01;

    @Autowired
    private TargetInterceptor02 targetInterceptor02;

    @Autowired
    private TargetInterceptor03 targetInterceptor03;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 调用 registry 对象的 addInterceptor() 方法添加拦截器对象
        // 默认情况下，拦截器添加之后，拦截所有请求
        registry.addInterceptor(myInterceptor01)
                .addPathPatterns("/private/res/*"); // 配置上面添加的拦截器拦截的请求范围

        registry.addInterceptor(myInterceptor02)
                .addPathPatterns("/public/res/02"); // 匹配规则：精确匹配

        registry.addInterceptor(myInterceptor03)
                .addPathPatterns("/fruit/**") // 匹配规则：模糊匹配
                .excludePathPatterns("/fruit/apple/sweet"); // 匹配规则：把一部分请求从拦截范围中排除

        registry.addInterceptor(targetInterceptor03)
                .addPathPatterns("/target")
                .order(3);
        registry.addInterceptor(targetInterceptor02)
                .addPathPatterns("/target")
                .order(2);
        registry.addInterceptor(targetInterceptor01)
                .addPathPatterns("/target")
                .order(1);
    }
}
